<?php

//列表页
class listMod extends commonMod
{
    public function __construct()
    {
        parent::__construct();
    }

    //栏目首页
    public function index()
    {
        $id = intval($_GET[0]);
        if (empty($id)) {
            $this->error404();
        }

        $info = $this->model->table('channel')->where('id=' . $id)->find(); //读取栏目信息

        if ($this->model->table('channel')->where('id=' . $id)->count()) {
        } else {
            $this->error404();
        }

        /*模块栏目地址自动纠正*/
        if ($info['way'] == 1 && $info['type'] == 0) {
            $model_info = $this->model->table('model')->where('id=' . $info['mid'])->find(); //查询栏目对应的模型信息
            if (!empty($model_info['forepart_channel_mod'])) {
                module($model_info['forepart_channel_mod'])->index($id);
                return;
            }
        }

        /*模块列表地址自动纠正*/
        if ($info['way'] == 1 && $info['type'] == 1) {
            $model_info = $this->model->table('model')->where('id=' . $info['mid'])->find(); //查询栏目对应的模型信息
            if (!empty($model_info['forepart_list_mod'])) {
                module($model_info['forepart_list_mod'])->index($id);
                return;
            }
        }

        /*模块地址纠正结束*/

        $nav=$this->nav_cid($info['id']); //位置导航


         if (!empty($info['url'])) //判断是否跳转
        {			
            $link=$this->tpl->compile(html_out($info['url']));
			header("HTTP/1.1 301 Moved Permanently");
			header("Location: ".$link."");
			exit;
        }

        $size = $info['size']; //每页显示的信息条数
        if (empty($size)) {
            $listRows = 10;
        } else {
            $listRows = $size;
        }

        $son_id = $this->getcat($info['id']); //查询此栏目下的所有栏目的id

		if($this->config['URL_HTML_MODEL']==1){
		$url = __APP__ . '/'.$info['pinyin'].'/index-{page}.html'; //分页基准网址
		}
		if($this->config['URL_HTML_MODEL']==2){
		$url = module('label')->getcurl($info['id']) . '/index-{page}.html'; //分页基准网址
		}
        $page = new Page();
        $cur_page = $page->getCurPage($url);
        $limit_start = ($cur_page - 1) * $listRows;
        $limit = $limit_start . ',' . $listRows;


        if ($info['type'] == 0) {
            $where = 'WHERE A.status=1 AND A.cid in (' . $son_id . ')';
        } else {
            $where = 'WHERE A.status=1 AND A.cid=' . $info['id'];
        }
        
        if ($info['ext_mid'] == 0) { //判断是否扩展模型
        $sql = "SELECT A.*,B.pid,B.type as ctype,B.name as cname,B.seoname as cseoname,B.pinyin as cpinyin FROM {$this->model->pre}content A LEFT JOIN {$this->model->pre}channel B ON A.cid=B.id {$where} ORDER BY A.hits DESC,A.sorter DESC, A.id DESC LIMIT {$limit}";
        }else{
        $ext_model=$this->model->table('plus_model')->where('id=' . $info['ext_mid'])->find();//获取扩展模型信息
        $sql = "SELECT A.*,C.*,B.pid,B.type as ctype,B.name as cname,B.seoname as cseoname,B.pinyin as cpinyin FROM {$this->model->pre}content A LEFT JOIN {$this->model->pre}channel B ON A.cid=B.id LEFT JOIN {$this->model->pre}{$ext_model['table']} C ON A.id = C.my_aid {$where} ORDER BY A.hits DESC,A.sorter DESC, A.id DESC LIMIT {$limit}";
        }
        $loop = $this->model->query($sql); //执行查询

        //计算数据总量
        $sql2 = "SELECT COUNT(*) FROM {$this->model->pre}content A LEFT JOIN {$this->model->pre}channel B ON A.cid = B.id {$where}";
        $count_menu = $this->model->query($sql2);
        $count = $count_menu[0]['COUNT(*)'];
        //计算结束

        $parent_info = $this->model->table('channel')->where('id=' . $info['pid'])->find(); //查询这个栏目的上级栏目信息
        if (empty($parent_info)) {
            $parent_info = array(
                "id" => "0",
                "pid" => "0",
                "mid" => "0",
                "name" => "无上级栏目");
        }

        $top = array_reverse($nav); //获取顶级栏目信息
        $top_id = $top[0]['id'];
        $top_info = $this->model->table('channel')->where('id=' . $top_id)->find(); //顶级栏目信息

        $this->assign('top_info', $top_info); //顶级栏目信息
        $this->assign('top_id', $top_id); //顶级栏目ID
        $this->assign('info', $info); //栏目信息
        $this->assign('loop', $loop); //内容循环
        $this->assign('nav', array_reverse($nav)); //位置导航
        $this->assign('parent_info', $parent_info); //上级栏目信息
        $this->assign('page', $this->page($url, $count, $listRows));
        
        if($this->config['LANG_OPEN']==1){
        $this->display(__LANG__.'/'.$info['tpl_list']);
        }else{
        $this->display($info['tpl_list']);
        }

    }



}

?>